﻿//  -------------------- iSharePoint -------------------- //
// 
//  Company:	IT Complex, LLC
//  Project:	Smart.Tests
//  File name:	TestEventManager.cs
//  Developer:	Solomatov Igor
//  Created:	03.04.2012
// 
//  -------------------- iSharePoint -------------------- //

using Microsoft.VisualStudio.TestTools.UnitTesting;
using Smart.Common;

namespace Smart.Documents.Tests
{
	[TestClass]
	public class TestEventManager
	{
		[TestMethod]
		public void EventManager()
		{
			string[] c = { "", "", "", "" };

			var res = SPH.RunUnder("http://localhost:777/",
								   delegate
								   {
									   var em = SPServices.EventManager;

									   em.Register<int>(null, "test.test1", 100,
														(eventName, arg1) =>
														{
															c[0] += eventName + arg1 + ":1";

															return true;
														});

									   em.Register<int>(null, "test.test1", 105,
														(eventName, arg1) =>
														{
															c[0] += eventName + arg1 + ":2";
															return true;
														});

									   em.Register<int, int>(null, "test.test2", 100,
															 (eventName, arg1, arg2) =>
															 {
																 c[1] += eventName + arg2 + ":1";
																 return true;
															 });

									   em.Register<int, int>(null, "test.test2", 105,
															 (eventName, arg1, arg2) =>
															 {
																 c[1] += eventName + arg2 + ":2";
																 return false;
															 });

									   em.Register<int, int, int>(null, "test.test3", 105,
																  (eventName, arg1, arg2, arg3) =>
																  {
																	  c[2] += eventName + arg3 + ":1";
																	  return true;
																  });

									   em.Register<int, int, int>(null, "test.test3", 100,
																  (eventName, arg1, arg2, arg3) =>
																  {
																	  c[2] += eventName + arg3 + ":2";
																	  return true;
																  });

									   em.Register<int, int, int, int>(null, "test.test4", 100,
																	   (eventName, arg1, arg2, arg3, arg4) =>
																	   {
																		   c[3] += eventName + arg4 + ":1";
																		   return false;
																	   });

									   em.Register<int, int, int, int>(null, "test.test4", 105,
																	   (eventName, arg1, arg2, arg3, arg4) =>
																	   {
																		   c[3] += eventName + arg4 + ":2";
																		   return true;
																	   });

									   em.FireEvent("test.test1", 1);
									   em.FireEvent("test.test2", 1, 2);
									   em.FireEvent("test.test3", 1, 2, 3);
									   em.FireEvent("test.test4", 1, 2, 3, 4);

									   em.Unregister("test.test1");
									   em.Unregister("test.test2");
									   em.Unregister("test.test3");
									   em.Unregister("test.test4");
								   });

			Assert.AreEqual(true, res, "SPH.RunUnder completed with errors!");

			Assert.AreEqual("test.test11:1test.test11:2", c[0]);
			Assert.AreEqual("test.test22:1test.test22:2", c[1]);
			Assert.AreEqual("test.test33:2test.test33:1", c[2]);
			Assert.AreEqual("test.test44:1", c[3]);
			//Assert.AreEqual("test.test55:2test.test55:1", c5);
		}
	}
}